package hu.elte.tudaskezelo.dbpedialike.activity;

import hu.elte.tudaskezelo.dbpedialike.R;
import hu.elte.tudaskezelo.dbpedialike.domain.CityInformation;
import hu.elte.tudaskezelo.dbpedialike.ws.NearestCity;
import hu.elte.tudaskezelo.dbpedialike.ws.SPARQLQuery;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.inject.Inject;

public class ViewCityInformationActivity extends RoboActivity {

	final String tag = "ViewCityInformationActivity";

	private double latitude;
	private double longitude;
	private CityInformation cityInformation;
	String city;

	@Inject
	public NearestCity nearestCity;
	@Inject
	public SPARQLQuery sparqlQuery;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info);
		Log.d(tag, "OnCreate");

		longitude = getIntent().getDoubleExtra("longitude", 0);
		latitude = getIntent().getDoubleExtra("latitude", 0);

		Log.d(tag, "longitude: " + longitude);
		Log.d(tag, "latitude: " + latitude);

		city = nearestCity.getNearestCity(longitude, latitude);

		Log.d(tag, city);

		cityInformation = sparqlQuery.cityQuery(city);

		showGraphics();

		Log.d(tag, cityInformation.toString());

	}

	@InjectView(R.id.textView1)
	TextView cityName;
	@InjectView(R.id.expandableListView1)
	ExpandableListView cityDetails;
//	@InjectView(R.id.textView2)
//	TextView cityPopulation;
//	@InjectView(R.id.textView4)
//	TextView cityHistory;
//	@InjectResource(R.id.cityImage)
	ImageView imageView;

	public void showCityPhoto(String photo) {
		Intent intent;
		intent = new Intent().setClass(this, ImageViewActivity.class);
		intent.putExtra("photo", photo);
		startActivity(intent);
	}
	
	public void showGraphics() {

		cityName.setText(cityInformation.getName());
		ExpandableListAdapter mAdapter;
        
        mAdapter = new MyExpandableListAdapter(cityInformation);
        cityDetails.setAdapter(mAdapter);
        
        cityDetails.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent,
                    View v, int groupPosition, int childPosition,
                    long id) {
                if (groupPosition == 0 && childPosition == 0) {
                   
                }
                return false;
            }
            });
		//cityPopulation.setText("Population: " + cityInformation.getPopulation());
		//cityHistory.setText(cityInformation.getHistory());

		try {
			ImageView i = (ImageView) findViewById(R.id.cityImage);
			Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
					cityInformation.getPhotoURL()).getContent());
			i.setImageBitmap(bitmap);
			i.setOnClickListener(new OnClickListener() {
		        @Override
		        public void onClick(View v) {
					showCityPhoto(cityInformation.getPhotoURL());
					}
		        });

		} catch (MalformedURLException e) {
			Log.e("Exception", "MalformedURLException");
		} catch (IOException e) {
			Log.e("Exception", "IOException");
		}

	}
	
	
	public class MyExpandableListAdapter extends BaseExpandableListAdapter {
		// Sample data set. children[i] contains the children (String[]) for
		// groups[i].
		private String[] groups = { "Population", "History" };
	    private String[][] children;

	    public MyExpandableListAdapter(CityInformation ci){
	    	children = new String[2][1];
	    	children[0][0] = String.valueOf(ci.getPopulation());
	    	children[1][0] = ci.getHistory();
	    }
	    public Object getChild(int groupPosition, int childPosition) {
	        return children[groupPosition][childPosition];
	    }

	    public long getChildId(int groupPosition, int childPosition) {
	        return childPosition;
	    }

	    public int getChildrenCount(int groupPosition) {
	        int i = 0;
	        try {
	        i = children[groupPosition].length;

	        } catch (Exception e) {
	        }

	        return i;
	    }

	    public TextView getGenericView() {
	        // Layout parameters for the ExpandableListView
	    	
	        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
	            ViewGroup.LayoutParams.WRAP_CONTENT, (ViewGroup.LayoutParams.WRAP_CONTENT));
	        TextView textView = new TextView(ViewCityInformationActivity.this);
	        textView.setLayoutParams(lp);
	        // Center the text vertically
	        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
	        // Set the text starting position
	        textView.setPadding(50, 0, 0, 0);
	        return textView;
	    }

	    @Override
	    public View getChildView(int groupPosition, int childPosition,
	        boolean isLastChild, View convertView, ViewGroup parent) {
	        TextView textView = getGenericView();
	        textView.setText(getChild(groupPosition, childPosition).toString());
	        return textView;
	    }

	    public Object getGroup(int groupPosition) {
	        return groups[groupPosition];
	    }

	    public int getGroupCount() {
	        return groups.length;
	    }

	    public long getGroupId(int groupPosition) {
	        return groupPosition;
	    }

	    @Override
	    public View getGroupView(int groupPosition, boolean isExpanded,
	        View convertView, ViewGroup parent) {
	        TextView textView = getGenericView();
	        textView.setText(getGroup(groupPosition).toString());
	        textView.setMinHeight(64);
	        return textView;
	    }

	    public boolean isChildSelectable(int groupPosition, int childPosition) {
	        return true;
	    }

	    public boolean hasStableIds() {
	        return true;
	    }

	}

}
